package bhbt.controller.admin.member;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.service.DailiService;
import bhbt.service.DailiShengService;
import bhbt.service.MemberService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






@Controller
 
public class LevelShengController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private DailiService dailiService;
	@Resource
	private DailiShengService dailiShengService;

	@RequestMapping("admin/member/levelShengShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//获取list
		Map formMap = getParameterMap(request);
		
		
		//接点图传过来的参数
		String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
		System.out.println("fatherLoginName:"+fatherLoginName);
		int qu = MapUtil.getIntValue(formMap, "qu");
		System.out.println("qu:"+qu);
		
		request.setAttribute("qu",qu);
		request.setAttribute("fatherLoginName",fatherLoginName);
		
		return "admin/member/levelSheng";
	}

	@RequestMapping("admin/member/levelSheng.shtml")
	public String update(HttpServletRequest request, ModelMap model) throws Exception{
	        
	    	
    	Map formMap = getParameterMap(request);
      
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
        
        boolean result =true;  //验证结果
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = memberService.executMemberByLoginName(loginName);
        
        String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
        
        Integer fatherId = null;
		int qu = MapUtil.getIntValue(formMap, "qu");
		
    	Admin admin = ContextUtil.getAdmin();
    	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
    	 
    	if(!admin.getTwoPassword().equals(pwd)){
    		setMessage(model,"管理员安全密码不正确。");
    		result=false;
    	}else{
    		int level = MapUtil.getIntValue(formMap, "level");
    		
    		 
    		if(member==null){
    			setMessage(model,"会员账号不存在。");
        		result=false;
    		}else{
    			if(member.getLevel()>=10){
    				setMessage(model,"该会员已经是代理。");
            		result=false;

    			}
    		}
    		
    		//接点人
    		
    		if(!fatherLoginName.equals("")&&fatherLoginName!=null){
    			Member father = memberService.executMemberByLoginName(fatherLoginName);
    			if(father != null){
    				
    				if(qu==1){
    					if(father.getZuoId()!=null){
    						setMessage(model,"该接点人左区已经有人了。");
    						result = false;
    					}
    				}
    				if(qu==2){
    					if(father.getYouId()!=null){
    						setMessage(model,"该接点人右区已经有人了。");
    						result = false;
    					}else {
							if(father.getZuoId()==null){
								setMessage(model,"该接点人左区没人，请先放左区。");
	    						result = false;
							}
						}
    					
    				}
    				
    				if(father.getLevel()<10){
    					setMessage(model,"该接点人不是代理，不能放。");
    					result = false;
    					
    				}
    				
    				fatherId = father.getId();
    			}else{
    				setMessage(model,"接点人不存在。");
    				result = false;
    				
    			}
    		}
			
		} 
    	if(!result){
    		
    		request.setAttribute("qu",qu);
    		request.setAttribute("fatherLoginName",fatherLoginName);
    		return "admin/member/levelSheng";
		}
    
		
    	
    	return "redirect:tuKuang.shtml?fatherLoginName="+fatherLoginName;
    }
	 
	 
}
